<?php
defined('BASEPATH') or exit('No direct script access allowed');

class StatisticsFeedBack extends AdminController
{

	function __construct()
	{
		parent::__construct();
		$this->sidebar = "Overview-Home";
		$this->load->model('Student_feedback_model');
		$this->load->model('Supervisor_feedback_model');
	}
	function Index()
	{
		// 控制筛选项部门
		$deptlist = (new AdminDeptList)->getRemark()[0];
		for ($i = 0; $i < count($deptlist); $i++) {
			$deptlist[$i] = '"' . $deptlist[$i] . '"';
		}
		$connar['items'] = 'distinct(dept_name)';
		$connar['where'] = $this->sqlEscape("(role_id=?? or find_in_set(??,sub_role_id))", array(WebRole::Teacher[XPHEnum::Value], WebRole::Teacher[XPHEnum::Value]));

		$connar['where'] .= " and dept_name in(" . implode(',', $deptlist) . ")";
		$dept_name = $this->Admin_model->get_list_full($connar);

		$new_dept_name = array();
		foreach ($dept_name as $k => $v) {
			if ($v['dept_name']) {
				$name = mb_substr($v['dept_name'], 0, 2, "utf-8");
				if ($name == '财经' || $name == '商贸' || $name == '交通' || $name == '鉴湖' || $name == '黄酒') {
					$new_dept_name[$k]['name'] = mb_substr($v['dept_name'], 0, 2, "utf-8");
				} else if ($name == '建筑' || $name == '机电' || $name == '交通') {
					$new_dept_name[$k]['name'] = mb_substr($v['dept_name'], 0, 4, "utf-8");
				} else if ($name == '设计') {
					$new_dept_name[$k]['name'] = mb_substr($v['dept_name'], 0, 5, "utf-8");
				} else {
					$new_dept_name[$k]['name'] = $v['dept_name'];
				}
				$new_dept_name[$k]['dept_name'] = $v['dept_name'];
			}
		}

		$data['dept_name'] = $new_dept_name;



		$this->load->view('admin/statisticsFeedBackList', $data);
	}
	function getDatas($params = array(), $isExport = false, $type = false)
	{
		// $where=$this->sqlEscape(' where del_flag=?? and feedback.status=?? and role_belong=??',array(CommonStatus::UnDeleted[XPHEnum::Value],FeedBackStatus::HaveWrite[XPHEnum::Value],WebRole::Manage[XPHEnum::Value]));
		//$where=$this->sqlEscape(' where admin_type=?? and del_flag=?? ',array(AdminType::Auth[XPHEnum::Value],CommonStatus::UnDeleted[XPHEnum::Value]));
		$where = $this->sqlEscape(' where del_flag=?? and (feedback.status=?? or feedback.status=??)', array(CommonStatus::UnDeleted[XPHEnum::Value], FeedBackStatus::HaveWrite[XPHEnum::Value], FeedBackStatus::End[XPHEnum::Value]));
		// $where=$this->sqlEscape(' where del_flag=?? and feedback.status=?? ',array(CommonStatus::UnDeleted[XPHEnum::Value],FeedBackStatus::HaveWrite[XPHEnum::Value]));

		$params = $_POST;
		if (isset($params['start_time']) && isset($params['end_time'])) {
			$where .= $this->sqlEscape(' and feedback.created>?? and feedback.created<??', array(strtotime($params['start_time'] . "00:00:00"), strtotime($params['end_time'] . "23:59:59")));
		}
		$role = explode(',', getSess()['sub_role_id']);
		array_push($role, getSess()['role_id']);

		if (in_array(WebRole::Admin[XPHEnum::Value], $role)  || in_array(WebRole::Manage[XPHEnum::Value], $role) || in_array(WebRole::SchoolLeader[XPHEnum::Value], $role)) {
			$where .= ' and 1=1 ';
		} else if (in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)) {
			$where .= $this->sqlEscape(' and db_admin.dept_name=?? ', array(getSess()['dept_name']));
		} else {
			$where .= $this->sqlEscape(' and feedback.creator=?? ', array(getSess()['id']));
		}

		if (!$isExport) {
			if (isset($params['pageNo']) && isset($params['pageSize'])) {
				$pageNo = $_POST['pageNo'] - 1;
				$pageSize = $_POST['pageSize'];
			} else {
				$pageNo = '0';
				$pageSize = '20';
			}
		}

		if (isset($params['dept_name'])  && $params['dept_name'] != '-1') {
			// 公共基础教学部，数据查找为公共基础教学部以及人文社科部
			if ($params['dept_name'] == '公共基础教学') {
				$where  .= " and (db_admin.dept_name like '%" . $params['dept_name'] . "%' or db_admin.dept_name like '%人文社科%')";
			} else {
				$where  .= " and db_admin.dept_name like '%" . $params['dept_name'] . "%'";
			}
		}

		if (isset($params['role_belong'])  && $params['role_belong'] != '-1') {
			$where  .= " and feedback.role_belong = '" . $params['role_belong'] . "'";
		}

		if (isset($params['creator'])  && $params['creator'] != '-1') {
			$where  .= " and db_admin.username like '%" . $params['creator'] . "%'";
		}

		//筛选
		if (isset($params['checkbox'])) {
			$where  .= " and db_admin.status = '" . CommonStatus::Deleted[XPHEnum::Value] . "'";
		}

		$where .= " and db_admin.dept_id != 0";

		if (!$isExport) {
			$sql = 'select feddback.*,db_admin.username from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback ' . $where . ' limit ' . $pageSize * $pageNo . ',' . $pageSize;
			$data['ret'] = $this->Student_feedback_model->query('select *,feedback.created as created_time from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback left join db_admin on db_admin.id=feedback.creator ' . $where . ' order by feedback.created desc' . ' limit ' . $pageSize * $pageNo . ',' . $pageSize);
			// var_dump($this->db->last_query());
			// exit();
			$count = $this->Student_feedback_model->query('select count(*) as count from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback left join db_admin on db_admin.id=feedback.creator ' . $where);
			$data['count'] = $count[0]['count'];
			$data['totalPage'] = ceil($data['count'] / $pageSize);
		} else {
			$sql = 'select feddback.*,db_admin.username from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback ' . $where;
			$data['ret'] = $this->Student_feedback_model->query('select *,feedback.created as created_time from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback left join db_admin on db_admin.id=feedback.creator ' . $where . ' order by feedback.created desc');
			$count = $this->Student_feedback_model->query('select count(*) as count from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback left join db_admin on db_admin.id=feedback.creator ' . $where);
			$data['count'] = $count[0]['count'];
		}


		if (isset($_POST['pageNo'])) {
			$data['pageNo'] = $_POST['pageNo'];
		}


		foreach ($data['ret'] as $k => $v) {
			if (!$isExport) {
				$data['ret'][$k]['created'] = date('Y-m-d H:i:s', $v['created_time']);
				if (strlen($v['content']) > 20) {
					$content1 = mb_substr($v['content'], 0, 20);
					$data['ret'][$k]['content1'] = $content1 . '...';
				} else {
					$data['ret'][$k]['content1'] = $v['content'];
				}
				if (strlen($v['result']) > 20) {
					$result1 = mb_substr($v['result'], 0, 20);
					$data['ret'][$k]['result1'] = $result1 . '...';
				}
			} else {
				$data['ret'][$k]['created'] = date('Y-m-d', $v['created_time']);
			}

			$data['ret'][$k]['type'] = (new SupervisorFeedbackType)->show($v['type']);
		}
		// exit(retJson('返回反馈列表',true,$result));
		// echo json_encode($data);

		// if (!$isExport) {
		// 	$data = $this -> Student_feedback_model -> get_page_list($connar);
		// } else {
		// 	$data["ret"] = $this -> Student_feedback_model -> get_list_full($connar);
		// }

		if (!$isExport) {
			//var_dump($this->db->last_query());
			//exit();

			echo json_encode($data);
		} else {
			// return $data["ret"];
			if ($type == 1) {
				$this->load->library('PHPWord/PHPWord');
				$PHPWord = new PHPWord();
				$section = $PHPWord->createSection();
				$PHPWord->addFontStyle('rStyle', array('bold' => true, 'color' => '000000', 'size' => 16));
				$PHPWord->addParagraphStyle('pStyle', array('align' => 'center'));
				$section->addText("反馈报表", 'rStyle', 'pStyle');
				$section->addTextBreak(2);
				$PHPWord->addFontStyle('fStyle', array('bold' => false, 'color' => '000000', 'size' => 12));
				$num = 0;
				foreach ($data['ret'] as $k => $v) {
					$num++;
					$section->addText($num . '.' . '【' . $v['username'] . '】' . '【' . $v['dept_name'] . '】' . '【' . $v['category'] . '】' . '【' . $v['content'] . '】' . '【' . $v['result'] . '】' . '【' . $v['created'] . '】');
					$section->addTextBreak(1);
				}


				$write = new PHPWord_Writer_Word2007($PHPWord);
				$director = "upload/export/" . date('Ymd', time()) . "/";
				if (!file_exists($director)) {
					//检查是否有该文件夹，如果没有就创建，并给予最高权限
					mkdir($director, 0777);
				}
				$file_name = '反馈报表' . date('Ymd') . '.docx';
				$write->save($director . iconv('utf-8', 'gb2312', $file_name));
				exit(retJson('返回地址', true, $director . $file_name));
			} else {

				$this->load->library('PHPExcel/PHPExcel');
				$excel = new PHPExcel();
				//公共样式
				$excel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(11);
				$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
				$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setWrapText(true);
				$excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(23);
				$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
				$excel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);

				$x_line = 0;
				$y_line = 1;

				//正式开始
				$excel = new PHPExcel();
				$excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(15);

				//标题
				$excel->getActiveSheet()->mergeCells(PHPExcel_Cell::stringFromColumnIndex(0) . '1:' . PHPExcel_Cell::stringFromColumnIndex(7) . '1');
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(0) . '1', '反馈报表');
				// $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(0).'1','反馈报表');
				$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
				$excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
				$excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16);
				$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
				$i = 2;
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(0) . $i, '序号');
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(1) . $i, '反馈人');
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(2) . $i, '部门');
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(3) . $i, '反馈类型');
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(4) . $i, '反馈内容');
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(5) . $i, '处理意见和结果');
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(6) . $i, '时间');



				$i++;
				$num = 0;
				foreach ($data['ret'] as $k => $v) {
					$num++;
					$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(0) . $i, $num);
					$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(1) . $i, $v['username']);
					$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(2) . $i, $v['dept_name']);
					$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(3) . $i, $v['category']);
					$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(4) . $i, $v['content']);
					$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(5) . $i, $v['result']);
					$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(6) . $i, $v['created']);

					$i++;
				}
				//设置对齐
				$excel->getActiveSheet()->getStyle('A1:' . PHPExcel_Cell::stringFromColumnIndex(9) . ($i - 1))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
				$excel->getActiveSheet()->getStyle('A1:' . PHPExcel_Cell::stringFromColumnIndex(0) . ($i - 1))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);


				$write = new PHPExcel_Writer_Excel2007($excel);
				$director = "upload/export/" . date('Ymd', time()) . "/";
				if (!file_exists($director)) {
					//检查是否有该文件夹，如果没有就创建，并给予最高权限
					mkdir($director, 0777);
				}
				$file_name = '反馈报表' . date('Ymdhis') . '.xlsx';
				$write->save($director . iconv('utf-8', 'gb2312', $file_name));
				exit(retJson('返回地址', true, $director . $file_name));
				// return $data["ret"];


			}
		}
	}
}
